define(function(require, exports, module) {
	var $ = require('$');
	var formcc = require('form');
	var nav = require("bootstrap_nav");
	var bootbox = require("bootbox");
	var store = require("store");
	var loader = require("./loader/loader");
	require("bootstrap-datetimepicker.min.css");
	// require("bootstrap_datetimepicker.zh-CN");
	require("bootstrap_datetimepicker");
	require('select2.css');
	require('select2');

	exports.init = function(divId, data) {
		var rechargerecord = {};
		if (data)
			rechargerecord = data;
		new formcc({
			attrs : {
				title : "话费充值修改",
				spansize : 'span12',
				id : "rechargerecordform",
				items : [{
          isText : true,
          label : "充值类型",
          id : 'type',
          name : 'type',
          type : "text",
          style : "width:50%",
          readonly : true,
          //value : rechargerecord.type,
          value : "企业充值",
          validations : {
            required : true
          }
        }, {
          isText : true,
          label : "ISV",
          id : 'isvid',
          name : 'isvid',
          type : "text",
          style : "width:50%",
          value : rechargerecord.isvid,
          readonly : false,
          validations : {
            required : true
          }
        },{
          isText : true,
          label : "APP",
          id : 'appid',
          name : 'appid',
          type : "text",
          style : "width:50%",
          value : rechargerecord.appid,
          readonly : false,
          validations : {
            required : true
          }
        },{
          isText : true,
          label : "企业",
          id : 'companyid',
          name : 'companyid',
          type : "text",
          style : "width:50%",
          readonly : false,
          placeholder : "请选择企业",
          value : rechargerecord.companyid
          
        /*  validations : {
            required : function(){
              
              if(rechargerecord.type == "isv")
                return false;
              
              if(rechargerecord.type == "company")
              
                return true;
            
            }
          }*/
          
        },{
          isText : true,
          label : "充值途径",
          id : 'rechargetype',
          name : 'rechargetype',
          type : "text",
          style : "width:50%",
          value : rechargerecord.rechargetype,
          placeholder : "请选择充值途径",
          validations : {
            required : true
          }
        },{
          isText : true,
          label : "业务单号",
          id : 'businessnum',
          name : 'businessnum',
          type : "text",
          style : "width:50%",
          value : rechargerecord.businessnum,
          placeholder : "请输入业务单号",
          validations : {
            required : true,
            minlength : 10
          }
        }, {
          isText : true,
          label : "充值金额（元）",
          id : 'amount',
          name : 'amount',
          type : "text",
          style : "width:50%",
          value : rechargerecord.amount,
          placeholder : "请输入充值金额",
          validations : {
            required : true
          }
        }, /*{
          isDatepicker : true,
          label : "充值日期",
          id : 'rechargetime',
          name : 'rechargetime',
          type : "text",
          style : "width:50%",
          value : rechargerecord.rechargetime,
          validations : {
            required : true,
            date : true
          }
        },*/ 
        {
          isText : true,
          label : "备注",
          id : 'remarks',
          name : 'remarks',
          type : "text",
          value : rechargerecord.remarks,
          style : "width:50%"
        }],
				buttons : [ {
					text : "保存",
					id : "btn_save",
					type : "submit",
					style : "btn btn-contrast",
					icon : "save"
				}, {
					text : "取消",
					id : "btn_cancel",
					style : "btn",
					type : "button",
					icon : "undo"
				} ]

			},
			renderTo : divId,
			afterRender : function() {
			  
	       //充值日期
        /*$('#rechargetime').datetimepicker({
          "startDate" : new Date()
        }).on('changeDate', function(ev) {
          $(this).attr("value", ev.localDate.valueOf());
        });*/
        
        // 初始化账户类型 默认正式
        /*$("#accounttype").select2({
          allowClear : true,
          multiple : false,
          type : "text",
          style : "width:50%",
          data : [ {
            id : 0,
            text : "正式"
          }, {
            id : 1,
            text : "调试"
          }
          ]
        });*/
			  
	       /*if(rechargerecord.type == "app"){
	          $('#companyid').attr("disabled","disabled");
	        }*/
	       
			// 初始化充值类型
      /*  $("#type").select2({
          allowClear : true,
          multiple : false,
          type : "text",
          style : "width:50%",
          data : [ {
            id : '2',
            text : "企业帐户充值"
          }, {
            id : '3',
            text : "赠送帐户充值"
          }
          , {
            id : 'persion',
            text : "用户个人充值"
          }
          ]
        });*/
        
     // 绑定isv名称下拉列表
        $.ajax(
         {
             type : "POST",
             dataType : "json",
             url : "/user/queryIsv.do?type=1",//GLOBAL.URL.ADMIN_GETCARRIEROPTIONS,// wp.huang
             async : false,
             success : function(data)
             {
              $("#isvid").select2({
                placeholder : "ISV名称",
                allowClear: true,
                    data: data
                  });
             },
             error : function(data)
             {
                 bootbox.alertTimeout('初始化ISV名称数据失败！');
             }
         });
        // 绑定APP下拉列表
        var getAppUrl = "/condition/getAppName.do?type=1";
        
       /* if(rechargerecord.type == "app"){   //APP充值时只能选择计费主体为APP的数据
          getAppUrl += "&paytype=0";
        }*/
        
        $("#appid").select2({
          placeholder: "请选择APP",
           allowClear: true,          
        initSelection: function (element, callback) {
                          callback({id: rechargerecord.appid, text: rechargerecord.appname});//这里初始化
                       },
      formatNoMatches: "没有匹配的APP",
                 ajax: {
                   url: getAppUrl,
                   dataType: 'json',
                   delay: 250,
                   data: function (term, pageNo) {
                     return {
                       q: " a.name like '%"+term+"%' ", // search term
                       isvid: $("#isvid").val(),
                       size: 25,
                       start: (pageNo-1)*25
                     };
                   },
                   results: function (data, pageNo) {
                     var dataObj = eval(data);
                     return {results:dataObj};
                   },
                   cache: true
                 }
          });
     // 绑定企业下拉列表
        $("#companyid").select2({
            placeholder: "请选择企业",
             allowClear: true,
          initSelection: function (element, callback) {
                           callback({id: rechargerecord.companyid, text: rechargerecord.companyname});//这里初始化
                         },
        formatNoMatches: "没有匹配的企业",
                   ajax: {
                     url: "/condition/getCompanyName.do",
                     dataType: 'json',
                     delay: 250,
                     data: function (term, pageNo) {
                       return {
                         q: " c.name like '%"+term+"%' and a.pay_type = '1' ", // search term
                         appid: $("#appid").val(),
                         size: 25,
                         start: (pageNo-1)*25
                       };
                     },
                     results: function (data, pageNo) {
                       var dataObj = eval(data);
                       return {results:dataObj};
                     },
                     cache: true
                   }
        });
        
        // 充值途径
        $("#rechargetype").select2({
          allowClear : true,
          multiple : false,
          type : "text",
          style : "width:50%",
          data : [ {
            id : 0,
            text : "对公转账"
          }/*, {
            id : 1,
            text : "其它"
          }*/
          ]
        });

				var theme = require('bootstrap_theme');
				theme.setValidateForm($("#rechargerecordform"), function() {
					var formdata = {
					  id : rechargerecord.id,
					  isvid : $("#isvid").val(),
	          type : $("#type").val(),
	          appid : $("#appid").val(),
	          rechargetype : $("#rechargetype").val(),
	          businessnum : $("#businessnum").val(),
	          companyid : $("#companyid").val(),
	          amount : $("#amount").val(),
	          rechargetime : $("#rechargetime").val(),
	          remarks : $("#remarks").val()
					};
					loader.loading("正在提交，请稍等……");
					$.ajax({
						url :"/isv/doEdit.do",// GLOBAL.URL.ADMIN_RETURNLICENSECARRIER,
						dataType : "json",
						data : "data=" + JSON.stringify(formdata),
						type : "POST",
						async : false,
						success : function(json) {
		          if(json.flag == '000000'){
	              nav.toPageView("admin/recharge.html");
	              loader.noloading();                  
		          } else {
		            oauth2Relogin();                    
		          }
						},
						error : function() {
							loader.noloading();
							bootbox.alertTimeout("网络请求失败，请重试！");
						}
					});
				});

				$('#btn_cancel').on('click', function() {
					//nav.toPageView("admin/recharge.html");
				  
				  $("#optForm").show();
          $("#seachdiv").show();
          $("#rechargelistdiv").show();
          $('#rechargeeditdiv').hide();
				});
				
			//ISV改变事件
        $("#isvid").on("change", function()
        {
          $('#appid').select2("val", "");
          $('#companyid').select2("val", "");
        });
        
        //APP改变事件
        $("#appid").on("change", function()
        {
          $('#companyid').select2("val", "");
        });
        
			}
		});
	};
});
